
(function($, win, doc){
	
	/**
	 *  默认值：/seeyon
	 */
	var appBasePath = (function(){
		
		var path = location.pathname;
		
		return path.substring(0, path.indexOf('/', 1));
	})();
	
	
	var app = new Vue({
		el: '#app',
		data: {
			basic:{
				
			},
			style:{
				selected: ''
			},
			listData:[{
				title: '...',
				jobName: '...',
				jobGroup: '',
				triggerName: '',
				triggerGroup: '',
				startTime: null,
				endTime: null,
				finalFireTime: null,
				previousFireTime: null,
				nextFireTime: null,
				jobStatus: null,
				cronExpression: '',
				description: '',
				logs: null
			}]
		},
		methods:{
			init: function(){
				
				this.loadListData();		
				
			},
			loadListData: function(){
				
				var _this = this;
				
				$.http.get({
					url: (appBasePath + '/snail/jobtask.do?method=scheduleList'),
					header: {'Accept': 'application/json'},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							if(result.data && 0 < result.data.length){
								
								// 补全属性
								for(var data in result.data){
					
									data['logs'] = [[[],[],[]],[[],[],[]],[[],[],[]]];
								}
								
								_this.listData = result.data;
							}
							
							_this.onChangeItem('collapse_item_0');
							
						}else{
							alert(result.message);
						}

					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onChangeItem: function(activeNames){

				if($.isBlank(activeNames)){
					return;
				}
				
				this.style.selected = '';
				
				var _this = this;
				var index = parseInt(activeNames.substring(14));
				var jobData = this.listData[index];
				
				$.http.get({
					url: (appBasePath + '/snail/jobtask.do?method=getJobData'),
					header: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json;charset=UTF-8'},
					data: {job_name: jobData.jobName, job_group: jobData.jobGroup},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.style.selected = '';
							
							_this.listData[index]['logs'] = (result.data ? result.data.logs : null);
							
							_this.style.selected = activeNames;
							
						}else{
							_this.$alert(result.message, '信息');
						}

					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onRun: function(index){
				
				var _this = this;
				var jobData = this.listData[index];
				
				$.http.post({
					url: (appBasePath + '/snail/jobtask.do?method=runSchedule'),
					header: {'Accept': 'application/json'},
					data: {job_name: jobData.jobName, job_group: jobData.jobGroup},
					onsuccess: function(result, status, xhr){
						
						_this.$alert(result.message, '信息');
						
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onPause: function(index){
				
				var _this = this;
				var jobData = this.listData[index];
				
				$.http.post({
					url: (appBasePath + '/snail/jobtask.do?method=pauseSchedule'),
					header: {'Accept': 'application/json'},
					data: {job_name: jobData.jobName, job_group: jobData.jobGroup},
					onsuccess: function(result, status, xhr){
												
						_this.$alert(result.message, '信息');
						
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onResume: function(index){
				
				var _this = this;
				var jobData = this.listData[index];
				
				$.http.post({
					url: (appBasePath + '/snail/jobtask.do?method=resumeSchedule'),
					header: {'Accept': 'application/json'},
					data: {job_name: jobData.jobName, job_group: jobData.jobGroup},
					onsuccess: function(result, status, xhr){
						
						_this.$alert(result.message, '信息');
						
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			onDelete: function(index){
				
				var _this = this;
				var jobData = this.listData[index];
				
				$.http.post({
					url: (appBasePath + '/snail/jobtask.do?method=deleteSchedule'),
					header: {'Accept': 'application/json'},
					data: {job_name: jobData.jobName, job_group: jobData.jobGroup},
					onsuccess: function(result, status, xhr){
						
						_this.$alert(result.message, '信息');
						
						win.location.reload(true);
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			toHtml: function(txt){
				
				if($.isBlank(txt)){
					return null;
				}
				
				return txt.replace(/[&]/g, '&amp;')
					.replace(/[<]/g, '&lt;')
					.replace(/[>]/g, '&gt;')
					.replace(/\r\n/g, '<br/>')
					.replace(/\r/g, '<br/>')
					.replace(/\n/g, '<br/>')
					.replace(/\t/g, '&nbsp;&nbsp;&nbsp;&nbsp;')
					.replace(/([^\W\s]*warn[^\W\s]*)/ig, '<span style="color: orange;">$1</span>')
					.replace(/([^\W\s]*error[^\W\s]*)/ig, '<span style="color:red;">$1</span>')
					.replace(/([^\W\s]*exception[^\W\s]*)/ig, '<span style="color:red;">$1</span>');
			},
			isBlank: function(txt){
				return $.isBlank(txt);
			},
			dateFormat: function(timestamp){
				
				return ($.isBlank(timestamp) ? null : $.dateFormat(new Date(parseInt(timestamp)), 'yyyy-MM-dd HH:mm:ss'));
			}
		},
		created: function(){
			this.init();
		}
	});
	
	
})(Snail, window, document);